﻿#region 信息

// Copyright©2014 Rex Rao all rights reserved.
// File name : Mojito\Mojito.Service\TableService.cs
// Author : Sohobloo
// Create time : 2014/08/05 16:07
// Update time : 2014/08/19 11:19

#endregion

#region 引用

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Mojito.Data.Repository;
using Mojito.Entity;
using Sohobloo.SoCommon;
using Sohobloo.SoCommon.CommonExtension;
using Sohobloo.SoMcCoffee.Entity;
using Sohobloo.SoMcCoffee.Service;

#endregion

namespace Mojito.Service.Impl
{
    [Export(typeof(ITableService))]
    internal class TableService : ServiceBase, ITableService
    {
        [Import]
        private ITableRepository TableRepository { get; set; }

        [Import]
        private IOrderRepository OrderRepository { get; set; }

        public Table Get(Guid tableId)
        {
            return TableRepository.GetByKey(tableId);
        }

        public IList<Table> GetList()
        {
            return TableRepository.Entities
                                  .Where(t => t.DataStatus != DataStatus.Deleted)
                                  .OrderBy(t => t.SortIndex)
                                  .ToList();
        }

        public OperationResult Add(string tableName, MinConsumptionType minConsumptionType, double? minConsumptionAmount)
        {
            ArgumentHelper.Instance.CheckEmpty(tableName);
            if(minConsumptionType != MinConsumptionType.None)
            {
                ArgumentHelper.Instance
                    .CheckNull(minConsumptionAmount)
                    .CheckRange(minConsumptionAmount.Value, 0, double.MaxValue);
            }
            var existed = TableRepository.Entities
                                         .Any(t => t.TableName == tableName
                                                   && t.DataStatus != DataStatus.Deleted);
            if(existed)
            {
                return new OperationResult(OperationResultType.Exist, "Table name exist");
            }

            var table = new Table
            {
                TableName = tableName,
                MinConsumptionType = minConsumptionType,
                MinConsumptionAmount = (minConsumptionType != MinConsumptionType.None)
                    ? minConsumptionAmount.Value
                    : 0
            };

            TableRepository.Insert(table);
            var result = Commit();
            result.AppendData = table;
            return result;
        }

        public OperationResult Update(Guid tableId, string tableName, MinConsumptionType minConsumptionType, double? minConsumptionAmount)
        {
            ArgumentHelper.Instance.CheckEmpty(tableName);
            if (minConsumptionType != MinConsumptionType.None)
            {
                ArgumentHelper.Instance
                    .CheckNull(minConsumptionAmount)
                    .CheckRange(minConsumptionAmount.Value, 0, double.MaxValue);
            }
            var existed = TableRepository.Entities
                                         .Any(t => t.TableName == tableName
                                                   && t.DataStatus != DataStatus.Deleted
                                                   && t.Id != tableId);
            if (existed)
            {
                return new OperationResult(OperationResultType.Exist, "Table name exist.");
            }
            var table = TableRepository.GetByKey(tableId);
            if(table.IsNull())
            {
                return new OperationResult(OperationResultType.NotExist, "Table not exist.");
            }

            table.TableName = tableName;
            table.MinConsumptionType = minConsumptionType;
            table.MinConsumptionAmount = (minConsumptionType != MinConsumptionType.None)
                ? minConsumptionAmount.Value
                : 0;

            TableRepository.Update(table);
            var result = Commit();
            result.AppendData = table;
            return result;
        }

        public OperationResult Delete(Guid tableId)
        {
            // 检查引用
            var referenced = OrderRepository.Entities
                                            .Any(o => o.TableId == tableId);
            if(referenced)
            {
                // 软删除
                var table = TableRepository.GetByKey(tableId);
                if (table.IsNull())
                {
                    return new OperationResult(OperationResultType.NotExist, "Table not exist.");
                }
                table.DataStatus = DataStatus.Deleted;
                TableRepository.Update(table);
            }
            else
            {
                // 硬删除
                TableRepository.Delete(tableId);
            }
            return Commit();
        }

        public OperationResult SortAfter(Guid tableId, Guid? prevTableId)
        {
            TableRepository.ChangeSort(tableId, prevTableId);
            return Commit();
        }
    }
}